/**
 * Created by Administrator on 2020/5/15.
 */
;
var helper = require("../../javascript/helper");
var db = require("../../javascript/mysql");
var request = require("request");


var name = "保倍多意外保险产品组合";
var mysql = new db.mysqlClient("accident");
//生成产品数据信息
function genProductInfo() {
    plan = {
        //年龄段
        "year": ["1-17", "18-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55"],
        //保障期限
        "days": ["70s"],
        //保障金额
        "money": ["100000", "150000", "300000", "500000"],
        //交费类型
        "payType": ["年交", "月交"],
        //交费间隔
        "interval": ["5", "10", "20"]

    };


    data = helper.product([plan.year, plan.days, plan.money, plan.payType, plan.interval]);
    for (i = 0; i < data.length; i++) {
        dt = (helper.formatProductInfo(data, plan));
        httpRequest(dt);


    }

}

//发送请求
function httpRequest(dt) {
    var url = "https://chanpin.metlife.com.cn/CriJ/calPrem.htm";
    //年龄
    age = (2020 - Number(dt.year.split("-")[0])).toString();
    //交费方式
    if (dt["payType"] == "年交") {
        payType = "12"
    } else {
        payType = "1"
    }
    //产品code
    if (dt["interval"] == "5") {
        productCode = "P000000915";
    } else if (dt["interval"] == "10") {
        productCode = "P000000916"

    } else {
        productCode = "P000000917";
    }
    params = {
        "appName": "",
        "appSex": "M",
        "appidType": "A",
        "appidNo": "",
        "appBirthday": "",
        "appPhone": "",
        "appPro": "110000",
        "appCity": "110100",
        "appAddress": "",
        "appEmail": "",
        "name": "",
        "sex": "M",
        "idType": "A",
        "idNo": "",
        "birthday": "" + age + "-01-01 ",
        "phone": "",
        "pro": "110000",
        "city": "110100",
        "address": "",
        "email": "",
        "source": "CUS000000046",
        "planCode": productCode,
        "planAmnt": dt["money"],
        "polTerm": "70",
        "poltermType": "A",
        "payPeriod": dt["interval"],
        "payIntv": payType,
        "relation": "0",
        "validate": ""

    };
    payload = helper.urlencode(params);
    request({
        url: url,
        method: "POST",
        json: false,
        headers: {
            "Accept": "text/plain, */*; q=0.01",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Cookie": "s_fid=0E801B69BF3EFB3C-29A685FC5E313B81; Hm_lvt_f5801180d60d23b0397ad4e08def2d65=1587708454,1588035226,1589510479,1589521510; s_cc=true; NSC_diboqjo_dt=ffffffff09ac1c6045525d5f4f58455e445a4a421548; JSESSIONID=000086efnTL7ISJ24yQS1dTTwJs:f7657e8f-a6ce-492e-b436-f2d715c0cf05; Hm_lpvt_f5801180d60d23b0397ad4e08def2d65=1589521545; s_sq=%5B%5BB%5D%5D",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest"
        },
        body: payload
    }, function (err, res, body) {
        if (!err && res.statusCode == 200) {
            if (body.indexOf("true") != -1) {
                price = (Number(JSON.parse(body).msg)).toFixed(2);
                data = {
                    "name": name,
                    "company": "大都会人寿",
                    "price": price
                };
                result = Object.assign( data,dt );
                mysql.insertData( result );
            }
        }

    })

}


